package thread;

class Counter {
    public static int count = 0;

    synchronized public void add() {
        count++;
    }


}

/**
 * 死锁问题
 * sychronized 是可重入锁
 */
public class demo10 {
    public static void main(String[] args) throws InterruptedException {
        Counter c = new Counter();
        Thread t = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (c) {
                    c.add();
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                c.add();
            }
        }, "t1");
        t.start();
        t1.start();
        t.join();
        t1.join();
        System.out.println(c.count);

    }


}
